package com.ruoyi.app.util;

import com.alibaba.fastjson.JSONObject;
import io.netty.handler.logging.LogLevel;
import lombok.extern.slf4j.Slf4j;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Slf4j
public class TimeValidityCheckUtil {

    private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static final SimpleDateFormat DATE_ONLY_FORMAT = new SimpleDateFormat("yyyy-MM-dd");

    public static boolean checkValidity(String validityType, String validityDetails, Date receiveTime) {
        switch (validityType) {
            case "AFTER_DAYS":
                return checkAfterDays(validityDetails, receiveTime);
            case "DATE_RANGE":
                return checkDateRange(validityDetails);
            case "SPECIFIC_DATES":
                return checkSpecificDates(validityDetails);
            default:
                return false;
        }
    }

    private static boolean checkAfterDays(String validityDetails, Date receiveTime) {
        JSONObject json = JSONObject.parseObject(validityDetails);
        int days = json.getIntValue("days");
        long oneDayInMillis = 24 * 60 * 60 * 1000;
        Date expirationDate = new Date(receiveTime.getTime() + days * oneDayInMillis);
        Date currentDate = new Date();
        return currentDate.before(expirationDate);
    }

    private static boolean checkDateRange(String validityDetails) {
        JSONObject json = JSONObject.parseObject(validityDetails);
        String startDateStr = json.getString("start_date");
        String endDateStr = json.getString("end_date");
        try {
            Date startDate = null;
            Date endDate = null;
            if (startDateStr.length() == 10) {
                // 长度为 10，推测格式为 yyyy-MM-dd
                startDate = DATE_ONLY_FORMAT.parse(startDateStr);
            } else if (startDateStr.length() == 19) {
                // 长度为 19，推测格式为 yyyy-MM-dd HH:mm:ss
                startDate = DATE_FORMAT.parse(startDateStr);
            } else {
                log.error("--------------时间格式有问题---------startDateStr:"+startDateStr);
            }
            if (startDateStr.length() == 10) {
                // 长度为 10，推测格式为 yyyy-MM-dd
                endDate = DATE_ONLY_FORMAT.parse(endDateStr);
            } else if (startDateStr.length() == 19) {
                // 长度为 19，推测格式为 yyyy-MM-dd HH:mm:ss
                endDate = DATE_FORMAT.parse(endDateStr);
            } else {
                log.error("--------------时间格式有问题---------endDateStr:"+endDateStr);
            }
            Date currentDate = new Date();
            return currentDate.after(startDate) && currentDate.before(endDate);
        } catch (ParseException e) {
            e.printStackTrace();
            return false;
        }
    }

    private static boolean checkSpecificDates(String validityDetails) {
        JSONObject json = JSONObject.parseObject(validityDetails);
        List<String> dates = json.getJSONArray("dates").toJavaList(String.class);
        Date currentDate = new Date();
        String currentDateStr = DATE_ONLY_FORMAT.format(currentDate);
        return dates.contains(currentDateStr);
    }

    public static void main(String[] args) {
        try {
            String validityType = "AFTER_DAYS";
            String validityDetails = "{\"days\": 7}";
            Date receiveTime = DATE_FORMAT.parse("2025-02-10 00:00:00");
            boolean isValid = checkValidity(validityType, validityDetails, receiveTime);
            System.out.println("当前时间是否有效: " + isValid);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}
